package com.htzz.collection.core.mongo;

import com.htzz.collection.domain.*;
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class QueryMongoTemplate {

    @Resource
    private MongoClient client;

    @Value("${mongo.database.query}")
    private String dataBase;

    private SimpleDateFormat sdfBegin = new SimpleDateFormat("yyyy-MM-dd 00:00:00.000");
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    private Map<String,DB> cache = new HashMap<>();

    private DB getDataBase(){
        if(null==cache.get(dataBase)){
            DB database = client.getDB(dataBase);
            cache.put(dataBase,database);
            return database;
        }
        return cache.get(dataBase);
    }

    /**
     * 条件查询数据，
     * @param date 查询结束时间
     * @param devId 设备
     * @param colId 采集点
     * @param hours 几个小时内
     * @param splitMillis 多少分钟分割一次 (能被60整除的数)
     * @return
     */
    public Map<String,Object> getHistoryData(Date date,String devId,String colId,Integer hours,Integer splitMillis){
        Map<String, Object> map = new HashMap<String, Object>();
        int i = 0;
        List<Map> rlist = new ArrayList<>();
        int totalCount=0;
        //循环次数
        while (i < (60/splitMillis*hours)) {

            BasicDBList condList = new BasicDBList();
            //设置条件
            BasicDBObject cond = new BasicDBObject();

            Calendar instance = Calendar.getInstance();
            instance.setTimeInMillis(date.getTime()- splitMillis*60*1000*(i+1));//i个splitMinllis分钟前
//            开始时间
            String beginTime=sdf.format(instance.getTime());
            cond.put("$gte", beginTime);// 大于等于开始时间
//            开始时间
            instance.setTimeInMillis(date.getTime()-splitMillis*60*1000*i);//i个splitMinllis分钟前
//            开始时间
            String endTime=sdf.format(instance.getTime());
            cond.put("$lte",endTime);// 小于等于
            //绑定条件字段
            BasicDBObject composeCod = new BasicDBObject();
            composeCod.put("t", cond);
            //塞入时间条件
            condList.add(composeCod);

            DB dataBase = getDataBase();
            DBCollection collection = dataBase.getCollection("t" + devId);
            DBObject orderBy = new BasicDBObject("t", -1);
            BasicDBObject qkCond = new BasicDBObject("_id",0);
            BasicDBObject kCond = new BasicDBObject("k", colId);
            //塞入采集点条件
            condList.add(kCond);


            // 查询条件组合
            BasicDBObject searchCond = new BasicDBObject();
            searchCond.put("$and", condList);
            Cursor cursor = collection.find(searchCond,qkCond).sort(orderBy).limit(1);
            while(cursor.hasNext()){
                DBObject next = cursor.next();
                Map toMap = next.toMap();
                rlist.add(toMap);
                totalCount++;
            }

            i++;
            System.out.println(i);
        }

        map.put("data",rlist);
        map.put("totalCount", totalCount);


        return map;
    }


    public Map<String,Object> getDatasByDevIdColIdTimesPage(QueryForDto1 query) {
        Map<String, Object> map = new HashMap<String, Object>();
        int totalCount = 0;
        DB dataBase = getDataBase();
        String devId = query.getDevId();
        DBCollection collection = dataBase.getCollection("t" + devId);
        BasicDBList condList = new BasicDBList();
        BasicDBObject cond = new BasicDBObject();
        Cursor cursor = null;
        DBObject orderBy = new BasicDBObject("t", -1);
        BasicDBObject equipment = new BasicDBObject("devId", devId);
        condList.add(equipment);
        if (!"".equals(query.getStartTime()) && query.getStartTime() != null) {
            cond.put("$gte", query.getStartTime());// 大于等于
        }
        if (!"".equals(query.getEndTime()) && query.getEndTime() != null) {
            cond.put("$lte", query.getEndTime());// 小于等于
        }
        if (cond.size() > 0) {
            BasicDBObject composeCod = new BasicDBObject();
            composeCod.put("t", cond);
            condList.add(composeCod);
        }
        // name条件
        BasicDBObject qkCond = new BasicDBObject("_id",0);
        if(null != query.getK()){
            BasicDBObject kCond = new BasicDBObject("k", query.getK());
            condList.add(kCond);
        }

        //分页信息
        if(query.getCurrentPage()==0){
            query.setCurrentPage(1);
        }
        if(query.getPagesize()==0||query.getPagesize()>5000){
            query.setPagesize(10);
        }

         // 查询条件组合
        BasicDBObject searchCond = new BasicDBObject();
        searchCond.put("$and", condList);
        totalCount = collection.find(searchCond).count();
        cursor = collection.find(searchCond,qkCond).skip((query.getCurrentPage() - 1) * query.getPagesize()).sort(orderBy).limit(query.getPagesize());

        List<Map> rlist = new ArrayList<>();
        while(cursor.hasNext()){
            DBObject next = cursor.next();
            rlist.add(next.toMap());
        }

        map.put("totalCount", totalCount);
        map.put("data",rlist);
//        map.put("success",true);
        return map;
    }


    public Map<String,Object> getLastDatasByDevIdColId(QueryParam2Dto query) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        DB dataBase = getDataBase();
        String devId = query.getDevId();
        DBCollection collection = dataBase.getCollection("t" + devId);
        DBObject orderBy = new BasicDBObject("t", -1);
        BasicDBObject qkCond = new BasicDBObject("_id",0);
        BasicDBObject kCond = new BasicDBObject("k", query.getK());
        Cursor cursor = collection.find(kCond,qkCond).sort(orderBy).limit(1);
        while(cursor.hasNext()){
            DBObject next = cursor.next();
            Map toMap = next.toMap();
//            Date tdate = sdf.parse(toMap.get("t").toString());
//            Date cdate = sdf.parse(toMap.get("createTime").toString());
//            toMap.put("t",tdate.getTime());
//            toMap.put("createTime",cdate.getTime());
            map.put("data",toMap);
        }
//        map.put("success",true);
        return map;
    }


    /**
     * 查询最新50条设备的状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getLast50StatusByDevId(QueryParam1Dto dto) throws Exception {
        QueryForDto1 queryDto=new QueryForDto1();
        queryDto.setDevId(dto.getDevId());
        queryDto.setK("state");
        queryDto.setPagesize(50);
        queryDto.setCurrentPage(1);

        return getDatasByDevIdColIdTimesPage(queryDto);
    }

    /**
     * 查询最新一条的设备状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getLastStatusByDevId(QueryParam1Dto dto) throws Exception {
        QueryParam2Dto queryDto=new QueryParam2Dto();
        queryDto.setDevId(dto.getDevId());
        queryDto.setK("state");
        return getLastDatasByDevIdColId(queryDto);
    }

    /**
     * 根据设备id和分页信息查询最新采集数据
     * @param dto
     * @return
     */
    public Map<String, Object> getDevDatasByDevIdPage(QueryParam3Dto dto)  throws Exception {

        QueryForDto1 queryDto=new QueryForDto1();
        queryDto.setDevId(dto.getDevId());
        queryDto.setPagesize((dto.getPagesize()==0||dto.getPagesize()>5000)?10:dto.getPagesize());
        queryDto.setCurrentPage(dto.getCurrentPage()==0?1:dto.getCurrentPage());

        return getDatasByDevIdColIdTimesPage(queryDto);
    }

    /**
     * 查询30分钟内的设备状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getStatusByDevId(QueryParam1Dto dto)  throws Exception {

        QueryForDto1 queryDto=new QueryForDto1();
        queryDto.setDevId(dto.getDevId());
        queryDto.setK("state");

        //数据量不能超过32M,默认只能查询30分钟内最多5000条数据，否则就的走分页查询接口
        queryDto.setPagesize(5000);
        queryDto.setCurrentPage(1);

        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(new Date().getTime()-30*60*1000);//30分钟前

        queryDto.setStartTime(sdf.format(instance.getTime()));
        queryDto.setEndTime(sdf.format(new Date()));

        return getDatasByDevIdColIdTimesPage(queryDto);
    }

    /**
     * 查询设备最新一条数据
     * @param dto
     * @return
     */
    public Map<String, Object> getLastTimeByDevId(QueryParam1Dto dto)  throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        DB dataBase = getDataBase();
        String devId = dto.getDevId();
        DBCollection collection = dataBase.getCollection("t" + devId);
        DBObject orderBy = new BasicDBObject("t", -1);
        BasicDBObject qkCond = new BasicDBObject("_id",0);
        BasicDBObject kCond = new BasicDBObject();
        Cursor cursor = collection.find(kCond,qkCond).sort(orderBy).limit(1);
        while(cursor.hasNext()){
            DBObject next = cursor.next();
            Map toMap = next.toMap();
            map.put("data",toMap);
        }
        return map;
    }

    /**
     * 查询指定设备的指定采集点24小时内的数据，每半个小时取第一条
     * @param dto
     * @return
     */
    public Map<String, Object> getHstoricalData(QueryParam2Dto dto) throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        map=getHistoryData(new Date(),dto.getDevId(), dto.getK(), 24,30);
        return map;
    }

    /**
     * 查询设备24小时内的状态数据（每半个小时一条）
     * @param dto
     * @return
     * @throws Exception
     */
    public Map<String, Object> getHstoricalStatus(QueryParam1Dto dto) throws Exception{
        QueryParam2Dto query=new QueryParam2Dto();
        query.setDevId(dto.getDevId());
        query.setK("state");
        return getHstoricalData(query);
    }

    /**
     * 查询指定时长内的采集数据（每30分钟一条）
     * @param dto
     * @return
     * @throws Exception
     */
    public Map<String, Object> getHstoricalDataByHours(QueryParam5Dto dto) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if(dto.getHours()>24){
            throw new Exception("查询时间范围不能大于24小时");
        }
        map=getHistoryData(new Date(),dto.getDevId(), dto.getK(), dto.getHours(),30);
        return map;
    }

    /**
     * 查询指定时长内的状态数据（每30分钟一条）
     * @param dto
     * @return
     * @throws Exception
     */
    public Map<String, Object> getHstoricalStatusByHours(QueryParam4Dto dto) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if(dto.getHours()>24){
            throw new Exception("查询时间范围不能大于24小时");
        }
        map=getHistoryData(new Date(),dto.getDevId(), "state", dto.getHours(),30);
        return map;
    }

    /**
     * 查询指定日期的设备数据
     * @param dto
     * @return
     */
    public Map<String, Object> getDevDatasByDevIdDate(QueryParam6Dto dto) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        if(dto.getDate()==null||"".equals(dto.getDate())){
            throw new Exception("请指定要查询的日期");
        }

        Date date=dto.getDate();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime());
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime()+24*60*60*1000);
        String endTime = sdf.format(instance.getTime());


        int totalCount = 0;
        DB dataBase = getDataBase();
        String devId = dto.getDevId();
        DBCollection collection = dataBase.getCollection("t" + devId);
        BasicDBList condList = new BasicDBList();
        BasicDBObject cond = new BasicDBObject();
        Cursor cursor = null;
        DBObject orderBy = new BasicDBObject("t", -1);
        BasicDBObject equipment = new BasicDBObject("devId", devId);
        condList.add(equipment);
        if (!"".equals(startTime) && startTime != null) {
            cond.put("$gte", startTime);// 大于等于
        }
        if (!"".equals(endTime) && endTime != null) {
            cond.put("$lte", endTime);// 小于等于
        }
        if (cond.size() > 0) {
            BasicDBObject composeCod = new BasicDBObject();
            composeCod.put("t", cond);
            condList.add(composeCod);
        }
        // name条件
        BasicDBObject qkCond = new BasicDBObject("_id",0);
//        if(null != dto.getK()){
//            BasicDBObject kCond = new BasicDBObject("k", dto.getK());
//            condList.add(kCond);
//        }

        //分页信息
        if(dto.getCurrentPage()==0){
            dto.setCurrentPage(1);
        }
        if(dto.getPagesize()==0||dto.getPagesize()>5000){
            dto.setPagesize(10);
        }

        // 查询条件组合
        BasicDBObject searchCond = new BasicDBObject();
        searchCond.put("$and", condList);
        totalCount = collection.find(searchCond).count();
        cursor = collection.find(searchCond,qkCond).skip((dto.getCurrentPage() - 1) * dto.getPagesize()).sort(orderBy).limit(dto.getPagesize());

        List<Map> rlist = new ArrayList<>();
        while(cursor.hasNext()){
            DBObject next = cursor.next();
            rlist.add(next.toMap());
        }

        map.put("totalCount", totalCount);
        map.put("data",rlist);


        return map;
    }

    /**
     * 查询设备、采集点指定日期的数据
     * @param dto
     * @return
     * @throws Exception
     */
    public Map<String, Object> getDevDatasByDevIdColIdDate(QueryParam7Dto dto)  throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        if(dto.getDate()==null||"".equals(dto.getDate())){
            throw new Exception("请指定要查询的日期");
        }

        Date date=dto.getDate();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime());
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime()+24*60*60*1000);
        String endTime = sdf.format(instance.getTime());


        int totalCount = 0;
        DB dataBase = getDataBase();
        String devId = dto.getDevId();
        DBCollection collection = dataBase.getCollection("t" + devId);
        BasicDBList condList = new BasicDBList();
        BasicDBObject cond = new BasicDBObject();
        Cursor cursor = null;
        DBObject orderBy = new BasicDBObject("t", -1);
        BasicDBObject equipment = new BasicDBObject("devId", devId);
        condList.add(equipment);
        if (!"".equals(startTime) && startTime != null) {
            cond.put("$gte", startTime);// 大于等于
        }
        if (!"".equals(endTime) && endTime != null) {
            cond.put("$lte", endTime);// 小于等于
        }
        if (cond.size() > 0) {
            BasicDBObject composeCod = new BasicDBObject();
            composeCod.put("t", cond);
            condList.add(composeCod);
        }
        // name条件
        BasicDBObject qkCond = new BasicDBObject("_id",0);
        if(null != dto.getK()){
            BasicDBObject kCond = new BasicDBObject("k", dto.getK());
            condList.add(kCond);
        }

        //分页信息
        if(dto.getCurrentPage()==0){
            dto.setCurrentPage(1);
        }
        if(dto.getPagesize()==0||dto.getPagesize()>5000){
            dto.setPagesize(10);
        }

        // 查询条件组合
        BasicDBObject searchCond = new BasicDBObject();
        searchCond.put("$and", condList);
        totalCount = collection.find(searchCond).count();
        cursor = collection.find(searchCond,qkCond).skip((dto.getCurrentPage() - 1) * dto.getPagesize()).sort(orderBy).limit(dto.getPagesize());

        List<Map> rlist = new ArrayList<>();
        while(cursor.hasNext()){
            DBObject next = cursor.next();
            rlist.add(next.toMap());
        }

        map.put("totalCount", totalCount);
        map.put("data",rlist);


        return map;

    }

    /**
     * 查询设备指定日期的状态数据
     * @param dto
     * @return
     * @throws Exception
     */
    public Map<String, Object> getStatusByDevIdDate(QueryParam6Dto dto) throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        QueryParam7Dto query=new QueryParam7Dto();
        query.setDevId(dto.getDevId());
        query.setK("state");
        query.setDate(dto.getDate());
        query.setPagesize(dto.getPagesize());
        query.setCurrentPage(dto.getCurrentPage());
        map=this.getDevDatasByDevIdColIdDate(query);
        return map;
    }

    /**
     * 按设备、采集点
     * @param devId
     * @param colId
     * @param value1 小于阀值
     * @param value2 大于阀值
     * @param type 1是取（value大于value1 and value小于value2）[传俩值]
     *             2.取（value小于value1 or|and value大于value2）[传1个值]
     *             3.取 (value == value1 || value2 需要传入null)
     * @param startTime
     * @param endTime
     * @return
     */
    public Map<String,Object> getDatasByDevIdAndColIdAndVAndT(String devId,String colId,
                                                              Object value1,Object value2,Integer type,
                                                              String startTime,String endTime,
                                                              Integer pagesize,Integer currentPage
                ){
        Map<String, Object> map = new HashMap<String, Object>();
        int totalCount = 0;
        DB dataBase = getDataBase();
        DBCollection collection = dataBase.getCollection("t" + devId);
        BasicDBList condList = new BasicDBList();
        BasicDBObject cond = new BasicDBObject();
        Cursor cursor = null;
        DBObject orderBy = new BasicDBObject("t", -1);
        BasicDBObject equipment = new BasicDBObject("devId", devId);
        condList.add(equipment);
        if (!"".equals(startTime) && startTime != null) {
            cond.put("$gte", startTime);// 大于等于
        }
        if (!"".equals(endTime) && endTime != null) {
            cond.put("$lte", endTime);// 小于等于
        }
        if (cond.size() > 0) {
            BasicDBObject composeCod = new BasicDBObject();
            composeCod.put("t", cond);
            condList.add(composeCod);
        }
        // name条件
        BasicDBObject qkCond = new BasicDBObject("_id",0);
        if(null != colId){
            BasicDBObject kCond = new BasicDBObject("k", colId);
            condList.add(kCond);
        }

        /**
         * 判断值 value1 value2
         */
        BasicDBObject condValue = new BasicDBObject();
        if(type==1){
            if(value1!=null){
                condValue.put("$gte", value1);// 大于等于
            }
            if(value2!=null){
                condValue.put("$lte", value2);// 小于等于
            }
            //存在值
            if (cond.size() > 0) {
                //如果v1 v2 同时有值请选择 type1 ,否则逻辑有问题需要修复 （v<v1 or v>v2 逻辑 ）
                BasicDBObject composeCodVlue = new BasicDBObject();
                composeCodVlue.put("v", condValue);
                condList.add(composeCodVlue);
            }
        }else if(type ==2){
            if(value1!=null){
                condValue.put("$lt", value1);// 小于
            }
            if(value2!=null){
                condValue.put("$gt", value2);// 大于
            }
            //存在值
            if (cond.size() > 0) {
                //如果v1 v2 同时有值请选择 type1 ,否则逻辑有问题需要修复 （v<v1 or v>v2 逻辑 ）
                BasicDBObject composeCodVlue = new BasicDBObject();
                composeCodVlue.put("v", condValue);
                condList.add(composeCodVlue);
            }
        }else{
            BasicDBObject composeCodVlue = new BasicDBObject();
            if(value1 instanceof Integer){
                composeCodVlue.put("v",Integer.valueOf(value1.toString()));
            }else if(value1 instanceof Double ){
                composeCodVlue.put("v",Double.valueOf(value1.toString()));
            }else if(value1 instanceof String ){
                composeCodVlue.put("v",value1.toString());
            }else{
                composeCodVlue.put("v",value1);
            }

            condList.add(composeCodVlue);
        }


        //分页信息
        if(currentPage==0){
            currentPage=1;
        }
        if(pagesize==0||pagesize>5000){
            pagesize=10;
        }

        // 查询条件组合
        BasicDBObject searchCond = new BasicDBObject();
        searchCond.put("$and", condList);
        totalCount = collection.find(searchCond).count();
        cursor = collection.find(searchCond,qkCond).skip((currentPage - 1) * pagesize).sort(orderBy).limit(pagesize);

        List<Map> rlist = new ArrayList<>();
        while(cursor.hasNext()){
            DBObject next = cursor.next();
            rlist.add(next.toMap());
        }

        map.put("totalCount", totalCount);
        map.put("data",rlist);
        return map;

    }

    /**
     * 查询小于某个值的数据
     * @param dto
     * @return
     * @throws Exception
     */
    public Map<String, Object> getLowDatasByDevIdColId(QueryParam8Dto dto)  throws Exception{

        Map<String, Object> map = new HashMap<String, Object>();
        map=this.getDatasByDevIdAndColIdAndVAndT(dto.getDevId(), dto.getK(),
                dto.getValue(),null,2,
                dto.getStartTime(),dto.getEndTime(),
                dto.getPagesize(),dto.getCurrentPage());
        return map;

    }

    /**
     * 查询大于某个值的数据
     * @param dto
     * @return
     * @throws Exception
     */
    public Map<String, Object> getHightDatasByDevIdColId(QueryParam8Dto dto) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map=this.getDatasByDevIdAndColIdAndVAndT(dto.getDevId(), dto.getK(),
               null, dto.getValue(),2,
                dto.getStartTime(),dto.getEndTime(),
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }


    /**
     * 查询等于某个值的数据
     * @param dto
     * @return
     */
    public Map<String, Object> getDatasByDevIdColIdV(QueryParam8Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        map=this.getDatasByDevIdAndColIdAndVAndT(dto.getDevId(), dto.getK(),
                dto.getValue(),null,3,
                dto.getStartTime(),dto.getEndTime(),
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }



    /**
     * 时间范围内分页查询设备指定状态数据
     * @param devId
     * @param value 状态值
     * @param startTime
     * @param endTime
     * @param  pagesize
     * @param  currentPage
     * @return
     */
    public Map<String,Object> getDatasByDevIdAndVAndT(String devId,Integer value,
                                                              String startTime,String endTime,
                                                              Integer pagesize,Integer currentPage
    ){
        Map<String, Object> map = new HashMap<String, Object>();
        int totalCount = 0;
        DB dataBase = getDataBase();
        DBCollection collection = dataBase.getCollection("t" + devId);
        BasicDBList condList = new BasicDBList();
        BasicDBObject cond = new BasicDBObject();
        Cursor cursor = null;
        DBObject orderBy = new BasicDBObject("t", -1);
        BasicDBObject equipment = new BasicDBObject("devId", devId);
        condList.add(equipment);
        if (!"".equals(startTime) && startTime != null) {
            cond.put("$gte", startTime);// 大于等于
        }
        if (!"".equals(endTime) && endTime != null) {
            cond.put("$lte", endTime);// 小于等于
        }
        if (cond.size() > 0) {
            BasicDBObject composeCod = new BasicDBObject();
            composeCod.put("t", cond);
            condList.add(composeCod);
        }
        // name条件
        BasicDBObject qkCond = new BasicDBObject("_id",0);
        BasicDBObject kCond = new BasicDBObject("k", "state");
        condList.add(kCond);

        BasicDBObject composeCodVlue = new BasicDBObject();
        composeCodVlue.put("v", value);
        condList.add(composeCodVlue);


        //分页信息
        if(currentPage==0){
            currentPage=1;
        }
        if(pagesize==0||pagesize>5000){
            pagesize=10;
        }

        // 查询条件组合
        BasicDBObject searchCond = new BasicDBObject();
        searchCond.put("$and", condList);
        totalCount = collection.find(searchCond).count();
        cursor = collection.find(searchCond,qkCond).skip((currentPage - 1) * pagesize).sort(orderBy).limit(pagesize);

        List<Map> rlist = new ArrayList<>();
        while(cursor.hasNext()){
            DBObject next = cursor.next();
            rlist.add(next.toMap());
        }

        map.put("totalCount", totalCount);
        map.put("data",rlist);
        return map;

    }

    /**
     * 查询今日设备停机状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getOfflineStatusByDevIdToday(QueryParam3Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        //开始时间
        String startTime = sdfBegin.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                4000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }



    /**
     * 查询设备今日故障状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getFaultStatusByDevIdToday(QueryParam3Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        //开始时间
        String startTime = sdfBegin.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                3000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

    /**
     * 查询设备今日待机状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getStandbyStatusByDevIdToday(QueryParam3Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        //开始时间
        String startTime = sdfBegin.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                2000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

    /**
     * 查询设备今日运行状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getRunStatusByDevIdToday(QueryParam3Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        //开始时间
        String startTime = sdfBegin.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                1000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

    /**
     * 查询今日设备数据条数
     * @param query
     * @return
     */
    public Map<String, Object> getDatasNumByDevIdToday(QueryParam1Dto query) {
        Map<String, Object> map = new HashMap<String, Object>();

        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        //开始时间
        String startTime = sdfBegin.format(instance.getTime());

        int totalCount = 0;
        DB dataBase = getDataBase();
        String devId = query.getDevId();
        DBCollection collection = dataBase.getCollection("t" + devId);
        BasicDBList condList = new BasicDBList();
        BasicDBObject cond = new BasicDBObject();
        BasicDBObject equipment = new BasicDBObject("devId", devId);
        //设备ID
        condList.add(equipment);
        //时间范围
        cond.put("$gte", startTime);// 大于等于
        cond.put("$lte", endTime);// 小于等于
        BasicDBObject composeCod = new BasicDBObject();
        composeCod.put("t", cond);
        condList.add(composeCod);
        // 查询条件组合
        BasicDBObject searchCond = new BasicDBObject();
        searchCond.put("$and", condList);
        totalCount = collection.find(searchCond).count();

        map.put("datasNum",totalCount);
        return map;
    }

    public Map<String, Object> getDatasNumByDevIdColIdToday(QueryParam2Dto query) {
        Map<String, Object> map = new HashMap<String, Object>();

        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        //开始时间
        String startTime = sdfBegin.format(instance.getTime());

        int totalCount = 0;
        DB dataBase = getDataBase();
        String devId = query.getDevId();
        DBCollection collection = dataBase.getCollection("t" + devId);
        BasicDBList condList = new BasicDBList();
        BasicDBObject cond = new BasicDBObject();
        BasicDBObject equipment = new BasicDBObject("devId", devId);
        //设备ID
        condList.add(equipment);
        //采集点
        BasicDBObject colId = new BasicDBObject("k", query.getK());
        condList.add(colId);

        //时间范围
        cond.put("$gte", startTime);// 大于等于
        cond.put("$lte", endTime);// 小于等于
        BasicDBObject composeCod = new BasicDBObject();
        composeCod.put("t", cond);
        condList.add(composeCod);
        // 查询条件组合
        BasicDBObject searchCond = new BasicDBObject();
        searchCond.put("$and", condList);
        totalCount = collection.find(searchCond).count();

        map.put("datasNum",totalCount);
        return map;
    }


    /**
     * 查询指定日期设备停机状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getOfflineStatusByDevIdDate(QueryParam6Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();

        Date date=dto.getDate();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime());
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime()+24*60*60*1000);
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                4000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }



    /**
     * 查询指定日期设备故障状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getFaultStatusByDevIdDate(QueryParam6Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=dto.getDate();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime());
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime()+24*60*60*1000);
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                3000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

    /**
     * 查询指定日期设备待机状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getStandbyStatusByDevIdDate(QueryParam6Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=dto.getDate();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime());
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime()+24*60*60*1000);
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                2000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

    /**
     * 查询指定日期设备运行状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getRunStatusByDevIdDate(QueryParam6Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=dto.getDate();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime());
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime()+24*60*60*1000);
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                1000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }




    /**
     * 查询指定小时内设备停机状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getOfflineStatusByDevIdHours(QueryParam9Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime()-dto.getHours()*60*60*1000);
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                4000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }



    /**
     * 查询指定小时内设备故障状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getFaultStatusByDevIdHours(QueryParam9Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime()-dto.getHours()*60*60*1000);
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                3000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

    /**
     * 查询指定小时内设备待机状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getStandbyStatusByDevIdHours(QueryParam9Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime()-dto.getHours()*60*60*1000);
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                2000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

    /**
     * 查询指定小时内设备运行状态数据
     * @param dto
     * @return
     */
    public Map<String, Object> getRunStatusByDevIdHours(QueryParam9Dto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date date=new Date();
        Calendar instance = Calendar.getInstance();
        //开始时间
        instance.setTimeInMillis(date.getTime()-dto.getHours()*60*60*1000);
        String startTime = sdf.format(instance.getTime());
        //结束时间
        instance.setTimeInMillis(date.getTime());
        String endTime = sdf.format(instance.getTime());

        map=this.getDatasByDevIdAndVAndT(dto.getDevId(),
                1000,
                startTime,endTime,
                dto.getPagesize(),dto.getCurrentPage());
        return map;
    }

}
